﻿@using ZX.Tools
@using ZX.Model
@model My_Ask
@{
    List<Dictionary> listType = ViewBag.ListType;
    Sys_UserModel user = ViewBag.User;
    List<Approval_Log> listLog = ViewBag.ListLog ?? new List<Approval_Log>();
    //List<Approval_User> listAuser = ViewBag.ListAUser ?? new List<Approval_User>();
    List<Approval> listApproval = ViewBag.ListApproval ?? new List<Approval>();
}
<link href="~/Static/styles/bootstrap.min.css" rel="stylesheet" />
<link href="~/Static/js/layer/need/layer.css" rel="stylesheet" />
<link href="~/Static/js/Validform/css/style.css" rel="stylesheet" />
<link href="~/Static/js/layui/css/layui.css" rel="stylesheet" />
<script src="/Static/js/jquery/jquery-1.11.1.min.js"></script>
<script src="~/Static/js/layui/layui.js"></script>
<script src="~/Static/js/jquery/form/min.js"></script>
<script src="~/Static/js/Validform/js/Validform_v5.3.2_min.js"></script>
<script src="~/Static/js/admin.min.js"></script>
<script src="~/Static/js/layer/layer.js"></script>
<script src="~/Static/js/layer/layer.tools.js"></script>
<script src="~/Static/res/My97DatePicker/WdatePicker.js"></script>
<style>
    .panel {
        border: 2px solid #e5e5e5 !important;
    }

    .perip-box {
        padding: 20px;
        margin-top: 10%;
    }

    .top-navbar-index {
        position: inherit;
        width: 100%;
        margin-bottom: 0;
    }

    .with-menu {
        padding: 20px;
    }
    .table > thead > tr > th, .table > tbody > tr > th, .table > tfoot > tr > th, .table > thead > tr > td, .table > tbody > tr > td, .table > tfoot > tr > td {
        font-size: 14px;
    }
</style>
@Html.Raw(ViewBag.SysAdminMainNavbar)
<div class='with-menu'>
    <div class='panel'>
        <div class='panel-heading'><strong><i class='icon-building'></i>员工请假申请单</strong></div>
        <div class='panel-body'>
            <form action="/My_Ask/SaveData" method="post" class="form" id="formMy_Ask">
                <input type="hidden" name="Id" value="@Model.Id" />
                <input type="hidden" name="Status" value="@Model.Status" />
                <table class='table table-bordered' style="border: 1px solid #ddd;">
                    <tr>
                        <th class="text-center w-120px">姓名</th>
                        <td>
                            @user.RealName
                        </td>
                        <th class='text-center w-50px'>部门</th>
                        <td>
                            @user.DeptName
                        </td>
                        <th class='text-center w-50px'>职位</th>
                        <td>
                            @user.PositionName
                        </td>
                    </tr>
                    <tr>
                        <th class="text-center w-120px">请假类型</th>
                        <td colspan="5">
                            @foreach (var item in listType)
                            {
                                <input type="checkbox" class="form-checkbox" name="AType" value="@item.Id" @(Model.AType == item.Id ? "checked" : "") datatype="*" nullmsg="请选择请假类型" />@item.Name
                                <span>&nbsp;&nbsp;</span>
                            }
                        </td>
                    </tr>
                    <tr>
                        <th class="text-center w-120px">事由</th>
                        <td colspan="5">
                            <textarea style="width:100%;" cols="3" name="ADesc" class='form-control' autocomplete="off" datatype="*" nullmsg="请填写事由">@Model.ADesc</textarea>
                        </td>
                    </tr>
                    <tr>
                        <th class="text-center w-120px">请假时间</th>
                        <td colspan="3">
                            <div class="form-group">
                                <div class="col-md-6 col-sm-10" style="padding-left:0;width: 388px;">
                                    <div class="input-group">
                                        <input type="text" class='form-control form-date' id="StartTime" placeholder="开始日期" name="StartTime" value="@Model.StartTime.ToDateFormat("yyyy-MM-dd")" onfocus="WdatePicker({dateFmt:'yyyy-MM-dd'})" style="width:100px;" onblur="getDateDiff()" datatype="*" nullmsg="请填写开始时间" />
                                        <input type="text" class='form-control form-date' id="stime" placeholder="时间" value="@Model.StartTime.ToDateFormat("HH:mm")" onfocus="WdatePicker({dateFmt:'HH:mm'})" style="width:60px;" onblur="getDateDiff()" />
                                        <span class="input-group-addon">截止日期</span>
                                        <input type="text" class='form-control form-date' id="EndTime" name="EndTime" placeholder="结束日期" value="@Model.EndTime.ToDateFormat("yyyy-MM-dd")" onfocus="WdatePicker({minDate:'#F{$dp.$D(\'StartTime\');}',dateFmt:'yyyy-MM-dd'})" style="width:100px;" onblur="getDateDiff()" datatype="*" nullmsg="请填写结束时间" />
                                        <input type="text" class='form-control form-date' id="etime" placeholder="时间" value="@Model.EndTime.ToDateFormat("HH:mm")" onfocus="WdatePicker({dateFmt:'HH:mm'})" style="width:60px;" onblur="getDateDiff()" />
                                    </div>
                                </div>
                            </div>
                        </td>
                        <td id="tdDay" colspan="2">@ViewBag.AppLen</td>
                    </tr>
                    @if (listLog.Count > 0)
                    {
                        <tr>
                            <th class="text-center w-120px">审批日志</th>
                            <td colspan="5">
                                <table>
                                    @foreach (var item in listLog)
                                    {
                                        <tr>
                                            <td style="border:0;">@(item.LContent + "，" + item.CreateTime.ToDateFormat("yyyy-MM-dd HH:mm:ss"))</td>
                                        </tr>
                                    }
                                </table>
                            </td>
                        </tr>
                    }
                    @*@if (listAuser.Count > 0)
                        {
                            <tr>
                                <th>请选择审批人</th>
                                <td colspan='9'>
                                    <select class="form-control" style="width:200px;" name="FK_ApprovalUserId" datatype="*" nullmsg="请选择审批人">
                                        <option value="">==请选择==</option>
                                        @foreach (var item in listAuser)
                                        {
                                            <option value="@item.Id" @(Model.FK_ApprovalUserId == item.Id ? "selected" : "")>@item.FlowName</option>
                                        }
                                    </select>
                                </td>
                            </tr>
                        }*@
                    <tr>
                        <th class="text-center w-120px">附件</th>
                        <td colspan="5">
                            <input type="hidden" value="@Model.MAttach" name="MAttach" id="MAttach" />
                            <button type="button" class="layui-btn" id="btnUpload">上传附件</button>
                            <a href="@Model.MAttach" target="_blank" id="aMAttach">@(Model.MAttach.IsNullOrEmpty() ? "" : "查看附件")</a>
                        </td>
                    </tr>
                    <tr>
                        <th>请选择审批流程<span style="color:red">*</span></th>
                        <td colspan='5'>
                            <select class="form-control" style="max-width:80%;" name="FK_ApprovalId" datatype="*" nullmsg="请选择审批流程">
                                <option value="">==请选择==</option>
                                @foreach (var item in listApproval)
                                {
                                    <option value="@item.Id" @(Model.FK_ApprovalId == item.Id ? "selected" : "")>@item.DName</option>
                                }
                            </select>
                        </td>
                    </tr>
                    <tr>
                        <th>&nbsp;</th>
                        <td colspan='5'>
                            <a href='javascript:;' class='btn btn-primary' onclick='saveCaogao()'>存为草稿</a>
                            &nbsp;&nbsp;
                            <a href='javascript:;' class='btn btn-primary' onclick='saveData()'>提交</a>
                            &nbsp;&nbsp;
                            <a href='javascript:;' class='btn btn-back' onclick='goBack()'>返回</a>
                            <span id="spmsg"></span>
                        </td>
                    </tr>
                </table>
                <input type="hidden" id="submitMsg" value="" />
            </form>
        </div>
    </div>
</div>
<script type="text/javascript">
    $(function () {
        $("#formMy_Ask").Validform({
            tiptype: function (msg, o, cssctl) {
                var objtip = $("#spmsg");
                cssctl(objtip, o.type);
                objtip.text(msg);
            },
            callback: function (form) {
                if ($("input[name='Status']").val() == "@ApplyStatus.草稿.ToInt()") {
                    win.showLoading("正在保存...");
                    if ($.trim($("#stime").val()) != "") $("#StartTime").val($("#StartTime").val() + " " + $("#stime").val());
                    if ($.trim($("#etime").val()) != "") $("#EndTime").val($("#EndTime").val() + " " + $("#etime").val());
                    $("#formMy_Ask").ajaxSubmit(function (data) {
                        if (data.Code == 300) {
                            win.alert(data.Message);
                        } else if (data.Code == 200) {
                            win.success($("#submitMsg").val(), function () {
                                goBack();
                            }, 1000);
                        }
                    });
                } else {
                    win.confirm("确认提交审批？", function () {
                        win.showLoading("正在保存...");
                        if ($.trim($("#stime").val()) != "") $("#StartTime").val($("#StartTime").val() + " " + $("#stime").val());
                        if ($.trim($("#etime").val()) != "") $("#EndTime").val($("#EndTime").val() + " " + $("#etime").val());
                        $("#formMy_Ask").ajaxSubmit(function (data) {
                            if (data.Code == 300) {
                                win.alert(data.Message);
                            } else if (data.Code == 200) {
                                win.success($("#submitMsg").val(), function () {
                                    goBack();
                                }, 1000);
                            }
                        });
                    });
                }
                return false;
            }
        });
        $(".form-checkbox").click(function () {
            $(this).prop("checked", true).siblings().prop("checked", false);
            $.post("/my_ask/GetAskFlow", { askType: $(this).val(),aid:"@Model.FK_ApprovalId" }, function (res) {
                if (res.Code == 200) {
                    $("select[name='FK_ApprovalId']").html(res.Data);
                }
            });
        });
        var askType = $("input[name='AType']:checked").val();
        if (askType) {
            $.post("/my_ask/GetAskFlow", { askType: askType,aid:"@Model.FK_ApprovalId" }, function (res) {
                if (res.Code == 200) {
                    $("select[name='FK_ApprovalId']").html(res.Data);
                }
            });
        }
    });
    $(function () {
        layui.use('upload', function () {
            var upload = layui.upload;
            var uploadInst1 = upload.render({
                elem: '#btnUpload'
                , url: '/upload/UploadAttach'
                , accept: 'file'
                , data: { direName: "/MyAsk/Files" }
                , done: function (res) {
                    if (res.Code == 200) {
                        $("#MAttach").val(res.Data);
                        $("#aMAttach").attr("href", res.Data).html("查看附件");
                        win.success("上传成功");
                    } else {
                        win.alert("上传失败");
                    }
                }
                , error: function (res) {
                    win.alert(JSON.stringify(res));
                }
            });
        });
    });
    function saveCaogao() {
        $("input[name='Status']").val("@(ApplyStatus.草稿.ToInt())");
        $("#submitMsg").val("保存草稿成功");
        $("#formMy_Ask").submit();
    }
    function saveData() {
        if(@Model.Id==0)$("input[name='Status']").val("@(ApplyStatus.新申请.ToInt())");
        $("#submitMsg").val("提交审批成功");
        $("#formMy_Ask").submit();
    }
    function goBack() {
        window.history.go(-1);
    }

    function getDateDiff() {
        if ($("#StartTime").val() != "" && $("#EndTime").val() != "") {
            var edate = new Date($("#EndTime").val());
            var mm = edate.getMonth() + 1;
            var dd = $.trim($("#stime").val()) == "" || $.trim($("#etime").val()) == "" ? edate.getDate() + 1 : edate.getDate();
            var etime = edate.getFullYear() + "-" + mm + "-" + dd;
            var btime = $("#StartTime").val() + ($.trim($("#stime").val()) != "" ? " " + $("#stime").val() + ":00" : "");
            etime = etime + ($.trim($("#etime").val()) != "" ? " " + $("#etime").val() + ":00" : "");
            var hh = $.admin.getDateDiff(btime, etime, "hh");
            dd = parseInt(hh / 24);
            hh = hh % 24;
            if (hh != 0) {
                if (dd > 0) {
                    $("#tdDay").html("共请假 " + dd + " 天" + hh + "小时");
                } else {
                    $("#tdDay").html("共请假 " + hh + "小时");
                }
            } else {
                $("#tdDay").html("共请假" + dd + " 天");
            }
        }
    }
</script>
